$(function() {
	var type = $("#rule_type").val();
    showOrHideOld(type);
})
    
/* 删除本行 */
function deleteCurrentRow(obj) {
	if (confirm("确定删除?")) {
		$(obj).parents('tr').remove();
	}
}
/* 添加到本行前 */
function addBefore(obj) {
	var tr = $(obj).parents('tr');
	tr.clone().insertBefore(tr);
}
/* 添加到本行之后 */
function addLater(obj) {
	var tr = $(obj).parents('tr');
	tr.clone().insertAfter(tr);
}

function submitAction(obj, type, tabid) {
	if(UTIL.checkButton(obj)) {
		var errMsg = checkVocabulary();
		if(errMsg) {
			alert(errMsg);
		} else {
			var formData = $('#actionSubmit').serialize();
			if(type == 1) {
				API.slangSentenceEdit(formData, function(res) {
					if (res.code == '0') {
						if(tabid && tabid=='28_2') {
							window.location.href = "/slang/sentence_list.htm?" + formData;
						} else {
							window.location.href = "/slang/index.htm?" + formData;
						}
						return true;
					} else {
						alert(res.desc);
					}
				});
			} else {
				API.slangRuleEdit(formData, function(res) {
					if (res.code == '0') {
						window.location.href = "/slang/rule_index.htm?" + formData;
						return true;
					} else {
						alert(res.desc);
					}
				});
			}
		}
		UTIL.releaseButton(obj);
		return false;
	}
}

function checkVocabulary() {
	var errMsg = "";
	var vs = $("#actionSubmit input[name=vocabulary]");
	var sentence = $("#sentence").val();
	var vStr = '';
	for(var i=0;i<vs.length;i++) {
		if(!$.trim($(vs[i]).val())) {
			return "分词不要有空值";
		}
		vStr += $.trim($(vs[i]).val());
	}
	if(sentence.indexOf("_") > 0) {
		sentence = sentence.substring(0, sentence.indexOf("_"));
	}
	if(vStr != sentence) {
		errMsg = "分词和句子不匹配";
	}
	return errMsg;
}

// 预览组词效果
function preview(obj) {
	if(UTIL.checkButton(obj)) {
		var errMsg = checkVocabulary();
		if(errMsg) {
			alert(errMsg);
		} else {
			var formData = $('#actionSubmit').serialize();
			API.slangSentencePreview(formData, function(res) {
				if (res.code == '0') {
					var preview = res.data.preview;
					$("#preview span").html(preview);
					$("#preview").show();
					return true;
				} else {
					alert(res.desc);
				}
			});
		}
		UTIL.releaseButton(obj);
		return false;
	}
}

function changeType(obj) {
	var type = $(obj).val();
	showOrHideOld(type);
}

function showOrHideOld(type) {
    if(type == 3) {
    	$(".update_property").show();
		$(".update_property_no").hide();
    } else {
    	$(".update_property").hide();
		$(".update_property_no").show();
    }
}
	